package com.vasthu.core.dao;

import com.vasthu.api.dao.UserDAO;
import com.vasthu.api.domain.User;
import java.util.*;
import java.sql.*;

public class UserDAOImpl implements UserDAO {

    public boolean insertUser(Connection con, User user) {
        if (user == null) {
            return false;
        }

        PreparedStatement pst = null;

        try {
            pst = con.prepareStatement("INSERT INTO User(user_username, user_password, user_name, "
                    + "user_address, user_city, user_nic, user_phone, user_email, user_role_id, "
                    + "user_salary, user_dob, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");

            pst.setString(1, user.getUserUserName());
            pst.setString(2, user.getUserPassword());
            pst.setString(3, user.getUserName());
            pst.setString(4, user.getUserAddress());
            pst.setString(5, user.getUserCity());
            pst.setString(6, user.getUserNIC());
            pst.setString(7, user.getUserPhone());
            pst.setString(8, user.getUserEmail());
            pst.setLong(9, user.getUserRoleId());
            pst.setFloat(10, user.getUserSalary());
            pst.setDate(11, new java.sql.Date(user.getUserDob().getTime()));
            System.out.println("Called > " + user.getUserSalary());
            pst.executeUpdate();

        } catch (Exception ex) {
            System.out.println("ERROR : UserDAOImpl->insertUser()");
        }

        return true;
    }

    public User getUserById(Connection con, long userId) {
        return new User();
    }

    public boolean deleteUserById(Connection con, long userId) {
        PreparedStatement pst = null;

        try {
            pst = con.prepareStatement("DELETE FROM User WHERE user_id = ?");
            pst.setLong(1, userId);
            pst.executeUpdate();
        } catch (Exception ex) {
            System.out.println("ERROR : UserDAOImpl->deleteUserById()");
        }

        return true;
    }

    public List<User> getAllUsers(Connection con) {
        List<User> userList = new ArrayList<User>();
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            pst = con.prepareStatement("SELECT * FROM User WHERE status = 1");
            rs = pst.executeQuery();

            while (rs.next()) {
                User user = new User();

                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setUserPhone(rs.getString("user_phone"));
                user.setUserEmail(rs.getString("user_email"));
                user.setUserAddress(rs.getString("user_address"));
                user.setUserCity(rs.getString("user_city"));
                user.setUserUserName(rs.getString("user_username"));
                user.setUserPassword(rs.getString("user_password"));
                user.setUserNIC(rs.getString("user_nic"));
                user.setUserRoleId(rs.getInt("user_role_id"));
                user.setUserSalary(rs.getFloat("user_salary"));
                user.setUserDob(rs.getDate("user_dob"));

                userList.add(user);
            }

        } catch (SQLException ex) {
            System.out.println("ERROR : UserDAOImpl->getAllUsers()");
        }

        return userList;
    }
}